package cn.ibizlab.odoo.core.odoo_sms.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Isms_api;
import cn.ibizlab.odoo.core.odoo_sms.domain.Sms_api;
import cn.ibizlab.odoo.core.odoo_sms.filter.Sms_apiSearchContext;
import cn.ibizlab.odoo.core.odoo_sms.service.ISms_apiService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[短信API] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Sms_apiServiceImpl implements ISms_apiService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Isms_apiClientService sms_apiClientService;

    @Override
    public Sms_api get(Integer id) {
        Isms_api clientModel = sms_apiClientService.createModel();
        clientModel.setId(id);
        sms_apiClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Sms_api get(Sms_api sms_api) {
        Sms_api retsms_api = this.get(sms_api.getId()) ;
    	BeanCopier copier=BeanCopier.create(retsms_api.getClass(), sms_api.getClass(), false);
        copier.copy(retsms_api, sms_api, null);
        return sms_api ;
    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Isms_api clientModel = sms_apiClientService.createModel();
        clientModel.setId(id);
        sms_apiClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public void updateBatch(List<Sms_api> sms_apis, int batchSize) {

        //sms_apiClientService.updateBatch(sms_apis,batchSize) ;

    }

    @Override
    public boolean create(Sms_api sms_api) {
        boolean bOk = false ;
        Isms_api clientModel = this.convert2Model(sms_api,null);
        sms_apiClientService.create(clientModel) ;
        this.convert2Domain(clientModel,sms_api);
        bOk = true ;

        return bOk ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //sms_apiClientService.removeBatch(sms_apis) ;

    }

    @Override
    public boolean update(Sms_api sms_api) {
        boolean bOk = false ;
        Isms_api clientModel = this.convert2Model(sms_api,null);
        sms_apiClientService.update(clientModel) ;
        this.convert2Domain(clientModel,sms_api) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public void createBatch(List<Sms_api> sms_apis, int batchSize) {

                        //sms_apiClientService.createBatch(sms_apis,batchSize) ;

    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Sms_api> searchDefault(Sms_apiSearchContext context) {
        List<Sms_api> list = new ArrayList<Sms_api>();
        Page<cn.ibizlab.odoo.core.client.model.Isms_api> clientModelList = sms_apiClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Isms_api clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Sms_api>(list, context.getPageable(), clientModelList.getTotalElements());
    }




    public Isms_api convert2Model(Sms_api domain , Isms_api model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = sms_apiClientService.createModel() ;
        }

         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
        return model ;
    }

    public Sms_api convert2Domain( Isms_api model ,Sms_api domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Sms_api();
        }

        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        return domain ;
    }


}
